package sk.fiit.vppj.eventmanager.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import sk.fiit.vppj.eventmanager.core.ConfigManager;
/**
 * Class for creating Settings window. 
 * @author Marek
 *
 */
final class Settings extends JFrame {

	ConfigManager config = new ConfigManager();
	String[] themes = { "nimbus", "windows", "metal" };
	String directory = null;
	private final static Logger log = Logger.getLogger( Settings.class.getName() );


	public Settings() {
		init();
	}

	private void init() {
		setDefaultCloseOperation( DISPOSE_ON_CLOSE );
		setSize( 300, 280 );
		setResizable( false );
		setLayout( new GridLayout( 0, 2, 0, 5 ) );

		showForm();
		setVisible( true );
	}

	//TODO refactoring needed
	private void showForm() {
		final Properties prop = this.config.getProperties();
		final JTextField dbhost = initTextField( "Database host: ", prop.getProperty( "dbhost" ), 20 );
		final JTextField dbname = initTextField( "Database name: ", prop.getProperty( "dbname" ), 20 );
		final JTextField dbuser = initTextField( "Database user: ", prop.getProperty( "dbuser" ), 20 );
		final JTextField dbpass = initTextField( "Database password: ", prop.getProperty( "dbpass" ), 20 );
		final JTextField dbport = initTextField( "Database port: ", prop.getProperty( "dbport" ), 4 );
		final JTextField dbuserssh = initTextField( "Database use SSH: ", prop.getProperty( "dbuserssh" ), 1 );
		directory = prop.getProperty( "photoLocation" );

		JLabel photolabel = new JLabel( "Photo location: " );
		JButton chooserButton = new JButton( "Select location" );
		chooserButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser( prop.getProperty( "photoLocation" ) );
				chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
				int returnValue = chooser.showOpenDialog( MainFrame.getInstance() );

				if( returnValue == JFileChooser.APPROVE_OPTION ) {
					try {
						directory = chooser.getSelectedFile().getCanonicalPath();
					}
					catch ( IOException e1 ) {
						log.log( Level.WARNING, "Photolocation doesn't exists!" );
						e1.printStackTrace();
					}
				}
			}
		} );
		add( photolabel );
		add( chooserButton );

		JLabel label = new JLabel( "Application theme : " );
		final JComboBox themeBox = new JComboBox( this.themes );
		themeBox.setSelectedItem( prop.getProperty( "theme" ) );
		add( label );
		add( themeBox );

		JButton saveButton = new JButton( "Save" );
		saveButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				config.setProperty( "dbhost", dbhost.getText() );
				config.setProperty( "dbname", dbname.getText() );
				config.setProperty( "dbuser", dbuser.getText() );
				config.setProperty( "dbpass", dbpass.getText() );
				config.setProperty( "dbport", dbport.getText() );
				config.setProperty( "dbuserssh", dbuserssh.getText() );
				config.setProperty( "photoLocation", directory );
				config.setProperty( "theme", themeBox.getSelectedItem().toString() );
				
			}

		} );
		add( saveButton );

		JButton cancelButton = new JButton( "Cancel" );
		cancelButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Settings.this.dispose();
			}
		} );

		add( cancelButton );

	}

	private JTextField initTextField(String labelName, String property, int length) {

		JLabel label = new JLabel( labelName );
		JTextField textField = new JTextField( property, length );
		add( label );
		add( textField );

		return textField;

	}

}
